<template>
  <div class="roleAuthTree">
    <div class="treeBox">
      <el-tree
        ref="roleAuthTree"
        node-key="id"
        :props="props"
        :data="menu"
        :check-strictly="strictly"
        show-checkbox
      ></el-tree>
    </div>
    <div style="text-align: end; margin-top: 10px">
      <el-button type="primary" @click="submitTree">确 认</el-button>
    </div>
  </div>
</template>

<script>
import { listMenu } from '@/api/system/menu'
export default {
  name: 'roleAuthTree',
  props: ['currentRole'],
  data() {
    return {
      menu: null,
      queryParams: {
        menuName: undefined,
        visible: undefined,
        ps: 9999,
        pn: 1,
      },
      props: {
        label: 'menuName',
        children: 'children',
      },
      list: null,
      authMenuIdList: [],
      strictly: false,
    }
  },
  watch: {
    currentRole(val) {
      // this.$refs.roleAuthTree.setCheckedKeys(val.menuId)
      this.strictly = true
      this.$nextTick(() => {
        this.$refs.roleAuthTree.setCheckedKeys(val.menuId)
        this.strictly = false
      })
    },
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询菜单列表 */
    getList() {
      let params = this.queryParams
      this.$depts.getMenu(params).then((res) => {
        if (res.code === 200) {
          this.menu = this.handleTree(res.data.rows, 'id')
          this.list = res.data.rows
          this.strictly = true
          this.$nextTick(() => {
            this.$refs.roleAuthTree.setCheckedKeys(this.currentRole.menuId)
            this.strictly = false
          })
        } else {
          this.$message.error(res.code + '-' + res.msg)
        }
      })
    },
    submitTree() {
      let list = this.$refs.roleAuthTree.getCheckedKeys()
      let list2 = this.$refs.roleAuthTree.getHalfCheckedKeys()
      this.$emit('getMenuList', list.concat(list2))
    },
  },
}
</script>

<style lang="less" scoped>
.roleAuthTree {
  position: relative;
  width: 100%;
  height: 400px;

  .treeBox {
    height: 360px;
    overflow: auto;
  }
}
</style>
